package top.maof.book.config;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import top.maof.book.spider.develop.BookPageModelPipeline;
import top.maof.book.spider.develop.ContentsPageModelPipeline;
import top.maof.book.spider.develop.book.k17.SeventeenK;
import top.maof.book.spider.develop.book.qidian.QiDian;
import top.maof.book.spider.develop.book.zongheng.ZongHeng;
import top.maof.book.spider.develop.contents.bqg.Biqusa;
import top.maof.book.spider.develop.contents.oula.Oula;
import top.maof.book.task.BookJob;

@Configuration
public class QuartzConfig {

    @Autowired
    BookPageModelPipeline bookPageModelPipeline;


    @Autowired
    ContentsPageModelPipeline contentsPageModelPipeline;


    @Bean
    public Scheduler scheduler() throws Exception {
        // Grab the Scheduler instance from the Factory
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        //启动
        scheduler.start();

        // book job
        submitQidian(scheduler);
        submitZongheng(scheduler);
        submitK17(scheduler);

        // contents Job
        submitOula(scheduler);
        submitBiqusa(scheduler);


        //关闭
        //scheduler.shutdown();
        return scheduler;
    }


    public void submitBiqusa(Scheduler scheduler) throws Exception {
        JobDataMap jobDataMap = new JobDataMap();
        jobDataMap.put("charset", Biqusa.CHARSET);
        jobDataMap.put("pipline", contentsPageModelPipeline);
        jobDataMap.put("clazz", Biqusa.class);
        jobDataMap.put("url", "https://www.biqusa.com/");

        //新建一个 Job WelcomeJob
        JobDetail job = JobBuilder.newJob(BookJob.class)
                .withIdentity("biqusaJob", "contentsJobGroup")
                .usingJobData(jobDataMap)
                .build();

        // 触发器 定义多长时间触发 JobDetail
        Trigger trigger = org.quartz.TriggerBuilder.newTrigger()
                .withIdentity("biqusaTrigger", "contentsTriggerGroup")
                .startNow()
                .withSchedule(CronScheduleBuilder.cronSchedule("0 0 2,5 ? * SAT"))
                .build();

        scheduler.scheduleJob(job, trigger);


    }




    public void submitOula(Scheduler scheduler) throws Exception {
        JobDataMap jobDataMap = new JobDataMap();
        jobDataMap.put("charset", Oula.CHARSET);
        jobDataMap.put("pipline", contentsPageModelPipeline);
        jobDataMap.put("clazz", Oula.class);
        jobDataMap.put("url", "https://www.58160.com/all.html");

        //新建一个 Job WelcomeJob
        JobDetail job = JobBuilder.newJob(BookJob.class)
                .withIdentity("oulaJob", "contentsJobGroup")
                .usingJobData(jobDataMap)
                .build();

        // 触发器 定义多长时间触发 JobDetail
        Trigger trigger = org.quartz.TriggerBuilder.newTrigger()
                .withIdentity("oulaTrigger", "contentsTriggerGroup")
                .startNow()
                .withSchedule(CronScheduleBuilder.cronSchedule("0 0 3,6 ? * MON"))
                .build();

        scheduler.scheduleJob(job, trigger);
    }



    public void submitQidian(Scheduler scheduler) throws Exception {
        JobDataMap jobDataMap = new JobDataMap();
        jobDataMap.put("charset", QiDian.CHARSET);
        jobDataMap.put("pipline", bookPageModelPipeline);
        jobDataMap.put("clazz", QiDian.class);
        jobDataMap.put("url", "https://www.qidian.com");

        //新建一个 Job WelcomeJob
        JobDetail job = JobBuilder.newJob(BookJob.class)
                .withIdentity("qidianJob", "bookJobGroup")
                .usingJobData(jobDataMap)
                .build();

        // 触发器 定义多长时间触发 JobDetail
        Trigger trigger = org.quartz.TriggerBuilder.newTrigger()
                .withIdentity("qidianTrigger", "bookTriggerGroup")
                .startNow()
                .withSchedule(CronScheduleBuilder.cronSchedule("0 0 0,2 ? * MON"))
                .build();
        scheduler.scheduleJob(job, trigger);
    }




    public void submitZongheng(Scheduler scheduler) throws Exception {
        JobDataMap jobDataMap = new JobDataMap();
        jobDataMap.put("charset", ZongHeng.CHARSET);
        jobDataMap.put("pipline", bookPageModelPipeline);
        jobDataMap.put("clazz", ZongHeng.class);
        jobDataMap.put("url", "http://book.zongheng.com/");

        //新建一个 Job WelcomeJob
        JobDetail job = JobBuilder.newJob(BookJob.class)
                .withIdentity("zonghengJob", "bookJobGroup")
                .usingJobData(jobDataMap)
                .build();

        // 触发器 定义多长时间触发 JobDetail
        Trigger trigger = org.quartz.TriggerBuilder.newTrigger()
                .withIdentity("zonghengTrigger", "bookTriggerGroup")
                .startNow()
                .withSchedule(CronScheduleBuilder.cronSchedule("0 0 0,2 ? * TUE"))
                .build();
        scheduler.scheduleJob(job, trigger);
    }




    public void submitK17(Scheduler scheduler) throws Exception {
        JobDataMap jobDataMap = new JobDataMap();
        jobDataMap.put("charset", SeventeenK.CHARSET);
        jobDataMap.put("pipline", bookPageModelPipeline);
        jobDataMap.put("clazz", SeventeenK.class);
        jobDataMap.put("url", "https://www.17k.com/");

        //新建一个 Job WelcomeJob
        JobDetail job = JobBuilder.newJob(BookJob.class)
                .withIdentity("17kJob", "bookJobGroup")
                .usingJobData(jobDataMap)
                .build();

        // 触发器 定义多长时间触发 JobDetail
        Trigger trigger = org.quartz.TriggerBuilder.newTrigger()
                .withIdentity("17kTrigger", "bookTriggerGroup")
                .startNow()
                .withSchedule(CronScheduleBuilder.cronSchedule("0 0 0,2 ? * WED"))
                .build();

        scheduler.scheduleJob(job, trigger);
    }

}
